package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "业务管理-物资资料相关接口")
@RestController
@RequestMapping("/business/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    //物资资料列表显示
    //http://www.localhost:8989/business/product/findProductList?pageNum=1&pageSize=6&name=111&categoryId=&supplier=&status=0&categorys=24,27,40
    @GetMapping("/findProductList")
    public ResponseBean<PageVO<Product>> findProductList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                         ProductVO product){

        List<Product> products = productService.findProductList(product.getName(),product.getStatus(),product.getCategoryKeys());
        List<Product> page = ListPageUtils.page(products, pageSize, pageNum);
        PageVO pageVO = null;
        if(page != null){
            pageVO = new PageVO(page.size(), page);
            return ResponseBean.success(pageVO);
        }else {
            List<Product> list = new ArrayList<>();
            pageVO = new PageVO(0,list);
            return ResponseBean.success(pageVO);
        }
    }

    @GetMapping("/findProducts")
    public ResponseBean<PageVO<Product>> findProducts(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                      ProductVO product){

        List<Product> products = productService.findProductList(product.getName(),product.getStatus(),product.getCategoryKeys());
        List<Product> page = ListPageUtils.page(products, pageSize, pageNum);
        PageVO pageVO = null;
        if(page != null){
            pageVO = new PageVO(page.size(), page);
            return ResponseBean.success(pageVO);
        }else {
            List<Product> list = new ArrayList<>();
            pageVO = new PageVO(0,list);
            return ResponseBean.success(pageVO);
        }
    }

    //TODO fastdfs
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Product product) {
        productService.add(product);
        return ResponseBean.success();
    }

    @GetMapping("/edit/{id}")
    public ResponseBean<Product> edit(@PathVariable Long id) {
        Product product = productService.edit(id);
        return ResponseBean.success(product);
    }

    //TODO
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody Product product){
        productService.update(id,product);
        return ResponseBean.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productService.delete(id);
        return ResponseBean.success();
    }

    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        productService.remove(id);
        return ResponseBean.success();
    }

    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        productService.back(id);
        return ResponseBean.success();
    }

    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        productService.publish(id);
        return ResponseBean.success();
    }

    @GetMapping("/findProductStocks")
    public ResponseBean<PageVO<ProductStockVO>> findAllProductStocks(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize,
            Long[] categoryKeys,
            String name){
        List<ProductStockVO> products = productService.findAllProductStocks(name,categoryKeys);
        List<ProductStockVO> page = ListPageUtils.page(products, pageSize, pageNum);
        PageVO pageVO = null;
        if(page != null){
            pageVO = new PageVO(page.size(), page);
            return ResponseBean.success(pageVO);
        }else {
            List<Product> list = new ArrayList<>();
            pageVO = new PageVO(0,list);
            return ResponseBean.success(pageVO);
        }
    }

    @GetMapping("/findAllStocks")
    public ResponseBean<List<ProductStockVO>> findAllStocks(ProductVO product){
        List<ProductStockVO> products = productService.findAllProductStocks(product.getName(),product.getCategoryKeys());
        return ResponseBean.success(products);
    }

}
